﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.IO;
using B2B.Models;

namespace B2B.Controllers
{
    public class HomeController : AlibabaApplicationController
    {
        //
        // GET: /Home/

        public ActionResult FileUpload(HttpPostedFileBase uploadFile)
        {
            if (uploadFile != null)
            {
                if (uploadFile.ContentLength > 0)
                {
                    string strSuffixFileName = "_" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss").Replace("_", "");
                    string FilePath = HttpContext.Server.MapPath("../Images/Alibaba") + "\\ " + strSuffixFileName + "_" + Path.GetFileName(uploadFile.FileName);
                    uploadFile.SaveAs(FilePath);
                    TempData["UploadValidationMessage_Success"] = "Data Upload Succeeded" + FilePath;

                    return View();
                }

                return View();
            }

            else
            {
                TempData["UploadValidationMessage_Failure"] = "Please provide the filename to be uploaded";
                return View();
            }

        }

        public ActionResult NotLogin()
        {
            return View();
        }

        public ActionResult Index()
        {
            int tongpage = 0;
            Models.SP_DanhSachSanPham sp_DanhSachSanPham = new Models.SP_DanhSachSanPham();
            ArrayList lis_SP = sp_DanhSachSanPham.DanhSachSanPham(1, out tongpage);
            ViewData["tongPage"] = tongpage;
            ViewData["listSP"] = lis_SP;
            return View();
        }

        public ActionResult IndexPhanTrang(int page)
        {
            int tongpage = 0;
            Models.SP_DanhSachSanPham sp_DanhSachSanPham = new Models.SP_DanhSachSanPham();
            ArrayList lis_SP = sp_DanhSachSanPham.DanhSachSanPham(page, out tongpage);
            ViewData["tongPage"] = tongpage;
            ViewData["listSP"] = lis_SP;
            return View("Index");
        }

        public JsonResult IndexPhanTrangJson(int page)
        {
            int tongpage = 0;
            Models.SP_DanhSachSanPham sp_DanhSachSanPham = new Models.SP_DanhSachSanPham();
            ArrayList lis_SP = sp_DanhSachSanPham.DanhSachSanPham(page, out tongpage);
            return Json(lis_SP, JsonRequestBehavior.AllowGet);
        }
      
        public JsonResult DanhSachDanhMuc(int id)
        {
            Models.DanhMuc dm = new Models.DanhMuc();
            List<Models.DanhMuc> list = dm.DanhSachDanhMuc(id);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult DangNhap(string email, string password)
        {
            SP_DangNhap dn = SP_DangNhap.dangNhap(email, password);
            if (dn != null)
            {
                Session["maTV"] = dn.MaTV;
                Session["email"] = email;
                Session["hoten"] = dn.HoTen;
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }

        public ActionResult DangXuat()
        {
            Session["maTV"] = null;
            Session["email"] = null;
            Session["hoten"] = null;
            return RedirectToAction("Index");
        }

        public ActionResult DangKy()
        {
            ViewData["ds_QG"] = SP_DanhSachQG.ds_QuocGia();
            return View();
        }

        public ActionResult AddUser(SP_DangKy dk)
        {
            int kq = SP_DangKy.DangKy(dk);
            if(kq == 1){
                Session["hoten"] = dk.HoTen;
                Session["email"] = dk.Email;
                return RedirectToAction("Index");
            }
            return View("Index");
        }



  

        public ActionResult MP4_Player()
        {
            return View();
        }

        public ActionResult Tivi()
        {
            return View();
        }

        
       
    }
}
